//
//  goveNewsVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/5/23.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "goveNewsVC.h"
#import "goveNewsCell.h"
#import "newsDetailVC.h"
#import <QuickLook/QuickLook.h>
static NSString *govecell = @"govecell";

@interface goveNewsVC ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UIAlertViewDelegate,QLPreviewControllerDataSource,QLPreviewControllerDelegate>
{
    NSInteger page;
    NSString *mhName;
    NSString *fileName;
    NSString *docName;
    NSMutableArray *docArr;
    
}
@property (weak, nonatomic) IBOutlet UISearchBar *searchbar;
@property (strong, nonatomic) UITableView *tableview;
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (strong, nonatomic) UILabel *linLabel;

@property (strong, nonatomic) NSMutableArray *viewArr;
@property (strong,nonatomic) NSString *documentsPath;
@property (strong, nonatomic) QLPreviewController * qlview;
@end

@implementation goveNewsVC
#pragma mark - 初始化
//doc路径
-(NSString *)documentsPath
{
    if (!_documentsPath) {
        _documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    }
    return _documentsPath;
}
-(NSMutableArray *)viewArr{
    if (!_viewArr) {
        _viewArr = [NSMutableArray array];
    }
    return _viewArr;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, self.searchbar.y + self.searchbar.height, screenW, screenH - self.searchbar.height - 64) style:UITableViewStylePlain];
    }
    return _tableview;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    if ([self.titleStr isEqualToString:@"政府公报"]) {
        self.searchbar.hidden = YES;
        self.tableview.y = 64;

        [self.tableview reloadData];

    }else if ([self.titleStr isEqualToString:@"投资政策"]){
        self.searchbar.hidden = YES;
        self.tableview.y = 64;

        [self.tableview reloadData];

    }else if ([self.titleStr isEqualToString:@"政策法规"]){

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = self.titleStr;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    self.view.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
    
    self.searchbar.delegate = self;
    
    fileName=[DOCUMENTPATHS stringByAppendingPathComponent:@"docFile.plist"];
    
    docArr=[[NSArray arrayWithContentsOfFile:fileName] copy];
    
    [self setTableView];
    
    page = 1;
    
    [self sendRequest];
    
    // 集成下拉加载更多
    [self setupDownRefresh];
    
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
}

-(void)setTableView{
    self.tableview.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.tableHeaderView.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
        self.tableview.tableFooterView.backgroundColor = [UIColor clearColor];
    self.tableview.contentSize = CGSizeMake(0, screenH);
    
    [self.view addSubview:self.tableview];
    UINib * nib = [UINib nibWithNibName:@"goveNewsCell" bundle:nil];
    [self.tableview registerNib:nib forCellReuseIdentifier:govecell];
}
-(void)setupDownRefresh{
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    // 设置刷新图片
    UIImage *images = [UIImage imageNamed:@"mj_footer"];
    NSArray *refreshingImages = @[images];
    [footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多内容了" forState:MJRefreshStateRefreshing];
    footer.stateLabel.font = [UIFont systemFontOfSize:12.0f];
    footer.stateLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0f];
    
    [footer setAutomaticallyRefresh:NO];
    
    self.tableview.mj_footer = footer;
    
}
-(void)loadMoreData{
    
    page++;
    [self sendRequest];
    
}
-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - searchbar delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    if (_dataArr.count != 0) {
        _dataArr = [NSMutableArray array];
        [self.tableview reloadData];
    }
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"%@",searchBar.text);
    
    if (searchBar.text.length == 0) {
        return YES;
    }else {
        page = 1;
        [self sendRequest];
        return YES;
    }
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self showRoundProgressWithTitle:@"加载中..."];
    NSLog(@"-----%@",searchBar.text);
    if (searchBar.text.length != 0) {
        [searchBar resignFirstResponder];
        page = 1;
        mhName = searchBar.text;
        [self sendRequest];
        searchBar.text = @"";
    }
    
}

#pragma mark - tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:_dataArr.count];
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.dataArr.count < 1) {
        
        return 0;
        
    }else{
        
    return 1;
        
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//        goveNewsCell *cell = [self tableView:self.tableview cellForRowAtIndexPath:indexPath];
//        //设置cell自适应高度
//        CGRect cellFrame = [cell frame];
//        cellFrame.origin = CGPointMake(0, 0);
//        CGFloat totalHeight = self.linLabel.height + 80;
//        cellFrame.size.height = totalHeight;
//        [cell setFrame:cellFrame];

        return 120 ;

}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if ([self.titleStr isEqualToString:@"政策法规"]){
        
        return 1;
        
    }else{
        
        return 1;
        
    }
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView * footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 1)];
    footView.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
    
    return footView;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    goveNewsCell * cell = [tableView dequeueReusableCellWithIdentifier:govecell];
    tableView.rowHeight = UITableViewAutomaticDimension;
    if (!cell) {
        cell = [[goveNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:govecell];
    }
    cell.downloadBtn.selected = NO;
    cell.downloadBtn.userInteractionEnabled = YES;
    cell.selectionStyle = NO;
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    cell.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
    
    cell.backView.tag = indexPath.section;
    
    NSDictionary *dataDict = self.dataArr[indexPath.section];
    
    //判断是否已下载
    NSMutableArray *docFileArr=[[NSArray arrayWithContentsOfFile:fileName] copy];
    for (NSDictionary *dic in docFileArr) {
        if ([dic[@"name"] isEqualToString:dataDict[@"title"]] && [dic[@"type"] isEqualToString:self.titleStr]) {
            cell.downloadBtn.selected = YES;
            cell.downloadBtn.userInteractionEnabled = NO;
        }
    }
    
    // toptitle
    cell.linLabel.text = dataDict[@"title"];
    cell.linLabel.numberOfLines = 0;
    cell.linLabel.textColor = [UIColor colorWithRed:0.0/255.0 green:178.0/255.0 blue:218.0/255.0 alpha:1];
    UIFont *font = [UIFont fontWithName:@"Arial" size:14];
    cell.linLabel.font = font;
    CGSize constraint = CGSizeMake(cell.backView.width - 10 - cell.downloadBtn.width, 2000.0f);
    CGSize size = [cell.linLabel.text sizeWithFont:font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    [cell.linLabel setFrame:CGRectMake(5, 5, size.width, size.height)];

    
    // 发文字号
    cell.fawenLabel.textColor = [UIColor lightGrayColor];

    if ([self.titleStr isEqualToString:@"投资政策"] || [self.titleStr isEqualToString:@"政策法规"]) {
        cell.fawenLabel.text = @"发文字号:";
    }else{
        cell.fawenLabel.text = @"下载次数:";
    }
 
    cell.fawen.textColor = [UIColor lightGrayColor];
    cell.fawen.font = [UIFont systemFontOfSize:12];
    
    if ([self.titleStr isEqualToString:@"投资政策"] || [self.titleStr isEqualToString:@"政策法规"]) {
        
        cell.fawen.text = dataDict[@"fawenzihao"];
        
    }else if ([self.titleStr isEqualToString:@"合同范本"]){
        
        cell.fawen.text = [NSString stringWithFormat:@"%@次",dataDict[@"why"]];
    }
    else{
        cell.fawen.text = [NSString stringWithFormat:@"%@次",dataDict[@"downNumber"]];
    }
    
    // 发布日期

    cell.timeLabel.textColor = [UIColor lightGrayColor];
    cell.timeLabel.font = [UIFont systemFontOfSize:12];
    cell.timeLabel.text = @"发布日期:";
 
    cell.time.textColor = [UIColor lightGrayColor];
    cell.time.font = [UIFont systemFontOfSize:12];
    if ([self.titleStr isEqualToString:@"政策法规"]) {
        
        cell.time.text = dataDict[@"chengwenTime"];
        
    }else if ([self.titleStr isEqualToString:@"合同范本"]){
        

        NSTimeZone *currentDateTimeZone = [NSTimeZone defaultTimeZone];
        NSDateFormatter *currentDateFormat = [[NSDateFormatter alloc]init];
        [currentDateFormat setTimeZone:currentDateTimeZone];
        [currentDateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
        NSDate *destDate = [currentDateFormat dateFromString:dataDict[@"createTime"]];
        NSString *dateString = [currentDateFormat stringFromDate:destDate];
        NSString *deteTimeStr = [dateString substringToIndex:16];
        cell.time.text = deteTimeStr;
    }
    else{
        cell.time.text = dataDict[@"createtime"];
    }
    
    
    cell.downloadBtn.tag = indexPath.section;
    
    for (NSDictionary * dict in docArr) {
        
        if ([dict valueForKey:dataDict[@"title"]]) {
            
            cell.downloadBtn.selected = YES;
            cell.downloadBtn.userInteractionEnabled = NO;
            
        }
        
    }
    
    
    [cell.downloadBtn addTarget:self action:@selector(down:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;



}
-(void)down:(UIButton *)btn{
    NSDictionary *dataDict = self.dataArr[btn.tag];
    
    if (allConstMemberid) {
        
        if ([self.titleStr isEqualToString:@"政策法规"] || [self.titleStr isEqualToString:@"投资政策"]) {
            
            [self showRoundProgressWithTitle:@"下载中..."];
            
            //远程地址
            NSURL *URL = [NSURL URLWithString:dataDict[@"downPath"]];
            
            NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
            
            AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
            
            NSURLRequest *request = [NSURLRequest requestWithURL:URL];
            
            NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {
                
                NSURL *downloadURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
                
                return [downloadURL URLByAppendingPathComponent:[response suggestedFilename]];
                
            } completionHandler:^(NSURLResponse *response,NSURL *filePath, NSError *error) {
                //此处已经在主线程了
                
                if (docArr) {
                    
                    //NSDictionary * dict = @{dataDict[@"title"]:[filePath path]};
                    //获取当前时间
                    NSDate *senddate = [NSDate date];
                    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
                    [dateformatter setDateFormat:@"YYYY.MM.dd"];
                    NSString *locationString = [dateformatter stringFromDate:senddate];
                    NSDictionary *dict = @{@"name":dataDict[@"title"],
                                           @"filePath":[filePath path],
                                           @"type":self.titleStr,
                                           @"downTime":locationString};
                    NSMutableArray * mtArr = [NSMutableArray array];
                    
                    for (NSDictionary * dict in docArr) {
                        
                        [mtArr addObject:dict];
                    }
                    
                    [mtArr addObject:dict];
                    
                    docArr = nil;
                    docArr = mtArr;
                    
                    [docArr writeToFile:fileName atomically:YES];
                    
                }else{
                    
                    //读取数据到字典
                    NSMutableArray *docFileArr = [NSMutableArray array];
                    
                    //NSDictionary * dict = @{dataDict[@"title"]:[filePath path]};
                    //获取当前时间
                    NSDate *senddate = [NSDate date];
                    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
                    [dateformatter setDateFormat:@"YYYY.MM.dd"];
                    NSString *locationString = [dateformatter stringFromDate:senddate];
                    NSDictionary *dict = @{@"name":dataDict[@"title"],
                                           @"filePath":[filePath path],
                                           @"type":self.titleStr,
                                           @"downTime":locationString};
                    [docFileArr addObject:dict];
                    
                    [docFileArr writeToFile:fileName atomically:YES];
                }
                
                
                btn.selected = YES;
                
                btn.userInteractionEnabled = NO;
                
                [self showRightWithTitle:@"下载成功!" autoCloseTime:2];
            }];
            
            [downloadTask resume];
            
        }else{
            
            [self showRoundProgressWithTitle:@"下载中..."];
            
            [LSHttpTool sendHttpData:dataDict[@"downPath"] params:nil success:^(id responseObject) {
                NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
                //远程地址
                NSURL *URL = [NSURL URLWithString:dicInfo[@"data"]];
                
                NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
                
                AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
                
                NSURLRequest *request = [NSURLRequest requestWithURL:URL];
                
                NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {
                    
                    NSURL *downloadURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
                    
                    return [downloadURL URLByAppendingPathComponent:[response suggestedFilename]];
                    
                } completionHandler:^(NSURLResponse *response,NSURL *filePath, NSError *error) {
                    //此处已经在主线程了
                    
                    if (docArr) {
                        
                        //NSDictionary * dict = @{dataDict[@"title"]:[filePath path]};
                        //获取当前时间
                        NSDate *senddate = [NSDate date];
                        NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
                        [dateformatter setDateFormat:@"YYYY.MM.dd"];
                        NSString *locationString = [dateformatter stringFromDate:senddate];
                        NSDictionary *dict = @{@"name":dataDict[@"title"],
                                               @"filePath":[filePath path],
                                               @"type":self.titleStr,
                                               @"downTime":locationString};
                        
                        NSMutableArray * mtArr = [NSMutableArray array];
                        
                        for (NSDictionary * dict in docArr) {
                            
                            [mtArr addObject:dict];
                        }
                        
                        [mtArr addObject:dict];
                        
                        docArr = nil;
                        docArr = mtArr;
                        
                        [docArr writeToFile:fileName atomically:YES];
                        
                    }else{
                        
                        //读取数据到字典
                        NSMutableArray *docFileArr = [NSMutableArray array];
                        
                        //NSDictionary * dict = @{dataDict[@"title"]:[filePath path]};
                        
                        //获取当前时间
                        NSDate *senddate = [NSDate date];
                        NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
                        [dateformatter setDateFormat:@"YYYY.MM.dd"];
                        NSString *locationString = [dateformatter stringFromDate:senddate];
                        NSDictionary *dict = @{@"name":dataDict[@"title"],
                                               @"filePath":[filePath path],
                                               @"type":self.titleStr,
                                               @"downTime":locationString};
                        [docFileArr addObject:dict];
                        
                        [docFileArr writeToFile:fileName atomically:YES];
                    }
                    
                    
                    btn.selected = YES;
                    
                    btn.userInteractionEnabled = NO;
                    
                    [self showRightWithTitle:@"下载成功!" autoCloseTime:2];
                    
                }];
                
                [downloadTask resume];
                
                
            } failure:^(NSError *error) {
                
                [self.tableview.mj_footer endRefreshing];
                
                
            }];
        
        }
        

    }else{
    
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登录", nil];
        [alert show];
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dataDict = self.dataArr[indexPath.section];
    
    fileName=[DOCUMENTPATHS stringByAppendingPathComponent:@"docFile.plist"];
    
    //读取数据到字典
    NSMutableArray *docFileArr=[[NSArray arrayWithContentsOfFile:fileName] copy];
    
    for (NSDictionary * dict in docFileArr) {
        
        //docName = [dict valueForKey:dataDict[@"title"]];
        if ([dict[@"name"] isEqualToString:dataDict[@"title"]]) {
            docName = dict[@"filePath"];
        }
        if (docName) {
            
            break;
        }
        
}
    
    if (docName) {
        self.qlview = [[QLPreviewController alloc]init];
        self.qlview.dataSource = self;
        self.qlview.delegate = self;
        self.qlview.currentPreviewItemIndex = 0;
        [self.navigationController pushViewController:self.qlview animated:YES];
        
    }
    else{
        NSString *urlStr;
        if ([self.titleStr isEqualToString:@"政府公报"]) {
            urlStr = @"govePublicNewsDetail_app_myHome.im";
            
        }else if ([self.titleStr isEqualToString:@"投资政策"]){
            urlStr = @"goveZhengceDetail_app_myHome.im";
            
        }else if ([self.titleStr isEqualToString:@"政策法规"]){
            urlStr = @"getPppPolicyDetail_app_ppp.im";
        }
        
        
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        
        dic[@"id"] = dataDict[@"id"];
        [LSHttpTool sendHttpData:urlStr params:dic success:^(id responseObject) {
            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
            newsDetailVC *news = [[newsDetailVC alloc]init];
            
            if ([self.titleStr isEqualToString:@"政策法规"]) {
                
                news.urlStr = dicInfo[@"data"][0][@"content"];
                news.titleStr = dicInfo[@"data"][0][@"title"];
                
            }else{
                
                news.urlStr = dicInfo[@"data"][@"content"];
                news.titleStr = dicInfo[@"data"][@"title"];
                
            }
            
            [self.navigationController pushViewController:news animated:YES];
            
        } failure:^(NSError *error) {
            
            [self.tableview.mj_footer endRefreshing];
            
            
        }];
    }
    
    
}
#pragma mark - 请求数据
-(void)sendRequest{
    
    NSString *urlStr;
    if ([self.titleStr isEqualToString:@"政府公报"]) {

        [self.tableview reloadData];
        urlStr = @"govePublicNews_app_myHome.im";
    }else if ([self.titleStr isEqualToString:@"投资政策"]){

        urlStr = @"goveZhengce_app_myHome.im";
    }else if ([self.titleStr isEqualToString:@"政策法规"]){
        
        urlStr = @"getPppPolicyList_app_ppp.im";
        
    }else if ([self.titleStr isEqualToString:@"合同范本"]){
        
        urlStr = @"getPppContractList_app_ppp.im";
    
    }
    
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    if (mhName) {
        
        dic[@"mhName"] = mhName;
        
    }
    dic[@"userId"] = self.userId;
    dic[@"page"] = @(page);
//    _dataArr = nil;
    [self.tableview reloadData];
    [LSHttpTool sendHttpData:urlStr params:dic success:^(id responseObject) {
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];

        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            if (page == 1) {
                
                _dataArr = nil;
                
                if ([self.titleStr isEqualToString:@"政策法规"] || [self.titleStr isEqualToString:@"合同范本"]){
                    
                     _dataArr = dicInfo[@"data"][@"record"];
                    
                }else{
                    
                    _dataArr = dicInfo[@"data"];
                    
                }
                

                [self.tableview reloadData];
                
            }else{
                NSArray *arr = [NSArray array];
                if ([self.titleStr isEqualToString:@"政策法规"] || [self.titleStr isEqualToString:@"合同范本"]){
                    
                    arr = dicInfo[@"data"][@"record"];
                    
                }else{
                    
                    arr = dicInfo[@"data"];
                    
                }

                NSMutableArray * mtArr = [NSMutableArray array];
                for (NSDictionary * dict in _dataArr) {
                    [mtArr addObject:dict];
                }
                for (NSDictionary * dict in arr) {
                    [mtArr addObject:dict];
                }
                _dataArr = nil;
                _dataArr = mtArr;
                
                [self.tableview reloadData];

            }
            
            [self.tableview.mj_footer endRefreshing];
            
        }else{
            
            if (_dataArr.count < 1) {
                
                self.tableview.rowHeight = screenH;

            }
            
            [self.tableview.mj_footer endRefreshing];
        }

        [self hideBubble];
        
    } failure:^(NSError *error) {
        
        [self.tableview.mj_footer endRefreshing];
        
    }];
}
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return 1;
}

-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    
    NSURL * myQLDocument = [NSURL fileURLWithPath:docName];
    return myQLDocument;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        //登录
        UIStoryboard *LoginAndRegister = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *loginNAV = [LoginAndRegister instantiateViewControllerWithIdentifier:@"LoginNav"];
        [self presentViewController:loginNAV animated:YES completion:nil];
    }
    
}
@end
